package com.xms.schema.extension;

import com.xms.schema.core.AttributeDefaults;
import com.xms.schema.core.AttributeTypeIds;
import com.xms.schema.entity.Attribute;
import com.xms.utils.StringUtil;

import java.util.Arrays;

/**
 * <p>
 * 字段信息扩展类
 * </p>
 *
 * @author migo
 * @since 2021/8/31
 */
public class AttributeExt {
    public static String getDbType(Attribute attr){
        String result = "";
        switch (attr.getAttributetypename())
        {
            case AttributeTypeIds.FLOAT:
            case AttributeTypeIds.MONEY:
            case AttributeTypeIds.SMALLMONEY:
                result = String.format("numeric(23,%d)", attr.getPrecision());
                break;

            case AttributeTypeIds.NVARCHAR:
                result = String.format("character varying(%d)", attr.getMaxlength());
                break;

            case AttributeTypeIds.TEXT:
            case AttributeTypeIds.NTEXT:
                result = "text";
                break;

            case AttributeTypeIds.VARCHAR:
                result = String.format("character varying(%d)", attr.getMaxlength());
                break;

            case AttributeTypeIds.CHAR:
                result = String.format("%s(%d)", AttributeTypeIds.CHAR, attr.getMaxlength());
                break;

            case AttributeTypeIds.NCHAR:
                result = String.format("%s(%d)", AttributeTypeIds.NCHAR, attr.getMaxlength());
                break;

            case AttributeTypeIds.BIT:
            case AttributeTypeIds.STATE:
                result = "boolean";
                break;

            case AttributeTypeIds.INT:
            case AttributeTypeIds.SMALLINT:
            case AttributeTypeIds.PICKLIST:
            case AttributeTypeIds.STATUS:
                result = "integer";
                break;

            case AttributeTypeIds.CUSTOMER:
            case AttributeTypeIds.OWNER:
            case AttributeTypeIds.PRIMARYKEY:
            case AttributeTypeIds.LOOKUP:
            case AttributeTypeIds.UNIQUEIDENTIFIER:
                result = "uuid";
                break;

            case AttributeTypeIds.TIMESTAMP:
            case AttributeTypeIds.SMALLDATETIME:
            case AttributeTypeIds.DATETIME:
                result = "timestamp(6) without time zone";
                break;

            default:
                result = attr.getAttributetypename();
                break;
        }
        return result;
    }

    public static boolean typeIsBit(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.BIT);
    }

    public static boolean typeIsInt(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.INT);
    }

    public static boolean typeIsSmallInt(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.SMALLINT);
    }

    public static boolean typeIsFloat(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.FLOAT);
    }

    public static boolean typeIsDecimal(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.FLOAT);
    }

    public static boolean typeIsMoney(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.MONEY);
    }

    public static boolean typeIsSmallMoney(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.SMALLMONEY);
    }

    public static boolean typeIsNvarchar(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.NVARCHAR);
    }

    public static boolean typeIsVarchar(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.VARCHAR);
    }

    public static boolean typeIsChar(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.CHAR);
    }

    public static boolean typeIsPickList(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.PICKLIST);
    }

    public static boolean typeIsOwner(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.OWNER);
    }

    public static boolean typeIsCustomer(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.CUSTOMER);
    }

    public static boolean typeIsPrimaryKey(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.PRIMARYKEY);
    }

    public static boolean typeIsState(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.STATE);
    }

    public static boolean typeIsStatus(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.STATUS);
    }

    public static boolean typeIsText(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.NTEXT);
    }

    public static boolean typeIsNText(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.NTEXT);
    }

    public static boolean typeIsUniqueidentifier(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.UNIQUEIDENTIFIER);
    }

    public static boolean typeIsDateTime(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.DATETIME);
    }

    public static boolean typeIsSmallDateTime(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.SMALLDATETIME);
    }

    public static boolean typeIsLookUp(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.LOOKUP);
    }

    public static boolean typeIsRef(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.LOOKUP)
                || attr.getAttributetypename().equals(AttributeTypeIds.OWNER)
                || attr.getAttributetypename().equals(AttributeTypeIds.CUSTOMER);
    }

    public static boolean typeIsPartyList(Attribute attr)
    {
        return attr.getAttributetypename().equals(AttributeTypeIds.PARTYLIST);
    }

    public static String getNameField(Attribute attr, String alias)
    {
        if (attr == null)
        {
            return null;
        }

        String field = StringUtil.notEmpty(alias) ? alias : attr.getName();
        if (typeIsLookUp(attr) || typeIsOwner(attr) || typeIsCustomer(attr) || typeIsState(attr)
                || typeIsBit(attr) || typeIsPickList(attr) || typeIsStatus(attr))
        {
            field += "name";
        }
        return field;
    }

    public static boolean isSystemControl(Attribute attr)
    {
        return Arrays.stream(AttributeDefaults.SYSTEM_ATTRIBUTES).anyMatch(x->x.equals(attr.getName()));
    }
}
